package cn.bdqn.layui.config;

import com.google.common.collect.Maps;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class DirectRabbitConfig {

    //队列 起名：TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);
        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue("order", true);
    }

    //Direct交换机 起名：TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
        //  return new DirectExchange("TestDirectExchange",true,true);
        return new DirectExchange("order.direct",true,false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("orders");
    }

    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }






    //队列 起名：TestDirectQueue
    @Bean
    Queue successEmailQueue() {
        return new Queue("email.queue", true);
    }

    //Direct交换机 起名：TestDirectExchange
    @Bean
    DirectExchange successEmailExchange() {
        //  return new DirectExchange("TestDirectExchange",true,true);
        return new DirectExchange("email.exchange", true, false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
    @Bean
    Binding successEmailBinding() {
        return BindingBuilder.bind(successEmailQueue()).to(successEmailExchange()).with("email.routing.key");
    }


    /**
     * 创建死信队列
     */
    @Bean
    public Queue successKillDeadQueue() {
        Map<String, Object> map = Maps.newHashMap();
        map.put("x-dead-letter-exchange", "kill.dead.exchange");
        map.put("x-dead-letter-routing-key", "kill.dead.routing.key");
        return new Queue("kill.read.queue", true, false, false, map);
    }

    //基本交换机
    @Bean
    public TopicExchange successKillDeadProdExchange() {
        return new TopicExchange("kill.dead.prod.exchange", true, false);
    }

    @Bean
    public Binding successKillDeadProdBinding() {
        return BindingBuilder.bind(successKillDeadQueue()).to(successKillDeadProdExchange()).with("kill.dead.prod.routing.key");
    }

    //创建真正的队列
    @Bean
    public Queue successKillRealQueue(){
        return  new Queue("kill.dead.real.queue",true);
    }

    //死信交换机
    @Bean
    public TopicExchange successKillRealExchange(){
        return new TopicExchange("kill.dead.real.exchange",true,false);
    }


    @Bean
    public Binding successKillRealBinding(){
        return  BindingBuilder.bind(successKillRealQueue()).to(successKillRealExchange()).with("kill.read.routing.key");
    }



}
